﻿using System.Threading.Tasks;

namespace KingerRobot.MexicoSailunMDC.Spi;
/// <summary>
/// 任务服务
/// </summary>
public interface ITaskService
{

    string GetPoductCodeByTaskNo(int taskNo);

    List<POWcsTask> GetTasksByGroupNo(int groupNo);
    List<POWcsTask> GetTasksByGroupNoAndScanner(int groupNo, long scannerLoc);
    int UpdateTaskIsOutScannerByTaskNo(int taskNo);
    /// <summary>
    /// 备份删除任务和指令
    /// </summary>
    /// <param name="taskId"></param>
    /// <returns></returns>
    int BakAndDeleteTaskAndCmd(long taskId);


    int BakAndDeleteCmd(long taskId);

    /// <summary>
    /// 删除异常出库口任务和指令
    /// </summary>
    /// <param name="taskNo"></param>
    /// <returns></returns>
    int BakAndDeleteOutAbnormalTaskAndCmdByTaskNo(int taskNo);

    int UpdateOutCmdStatus();

    /// <summary>
    /// 获取已执行的指令
    /// </summary>
    /// <returns></returns>
    List<POWcsTaskCmd> GetExecutedTaskCmds();
    /// <summary>
    /// 测试用
    /// </summary>
    /// <returns></returns>
    List<POWcsTaskCmd> GetCmdTestV5();
    /// <summary>
    /// 通过任务Id获取任务编号
    /// </summary>
    /// <param name="taskId"></param>
    /// <returns></returns>
    int GetTaskNoByTaskId(long taskId);
    /// <summary>
    /// 删除任务命令
    /// </summary>
    /// <param name="taskId">任务ID</param>
    /// <returns></returns>
    int BakAndDeleteCmdByTaskId(long taskId);
    /// <summary>
    /// 通过任务Id获取任务
    /// </summary>
    /// <param name="taskId"></param>
    /// <returns></returns>
    BizTaskBean GetTaskByTaskId(long taskId);

    /// <summary>
    /// 查找所有入库未执行任务
    /// </summary>
    /// <returns></returns>
    List<TaskInfo> GetAllInTasks();
    /// <summary>
    /// 查找所有出库未执行任务
    /// </summary>
    /// <returns></returns>
    List<TaskInfo> GetAllOutTasks();
    /// <summary>
    /// 是否喷涂
    /// </summary>
    /// <param name="taskNo"></param>
    /// <returns></returns>
    int IsSprayByTaskNo(int taskNo);
    /// <summary>
    /// 通过条码获取可执行信息
    /// </summary>
    /// <param name="productCode"></param>
    /// <returns></returns>
    BizTaskBean GetTaskBeanByExecuteProductCode(string productCode);
    /// <summary>
    /// 异常入库 开始位置查找
    /// </summary>
    /// <param name="startLoc"></param>
    /// <returns></returns>
    BizTaskBean GetTaskBeanByExecuteStartLoc(long startLoc);
    /// <summary>
    /// 获得出库订单类型（大于0是出道立库库位；等于0是其他）
    /// </summary>
    /// <param name="targerCmdLoc"></param>
    /// <returns></returns>
    int CheckOutTaskType(long targerCmdLoc);
    /// <summary>
    /// 根据堆垛机ID得到未执行的入库任务
    /// </summary>
    /// <returns></returns>
    List<BizTaskBean> GetAllInExecuteTaskByEquipId(long equipId);
    /// <summary>
    /// 根据堆垛机ID得到已执行入库任务
    /// </summary>
    /// <returns></returns>
    List<BizTaskBean> GetAllInExecutedTaskByEquipId(long equipId);

    /// <summary>
    /// 流水号获取执行起始位置
    /// </summary>
    /// <param name="taskNo"></param>
    /// <returns></returns>
    long GetCmdStartLocByTaskNo(int taskNo);

    /// <summary>
    /// 获取任务类型
    /// </summary>
    /// <param name="taskNo"></param>
    /// <returns></returns>
    int GetTaskTypeByTaskNo(int taskNo);
    /// <summary>
    /// 判断设备是否有未完成指令
    /// </summary>
    /// <param name="equipId"></param>
    /// <returns></returns>
    string GetTaskByEquipId(long equipId);
    /// <summary>
    /// 根据堆垛机ID得到未执行的任务指令[正常任务、异常任务]
    /// </summary>
    /// <returns></returns>
    List<BizTaskBean> GetAllExecuteTaskByEquipId(long equipId);
    /// <summary>
    /// 根据堆垛机ID得到未执行的任务指令[正常任务、异常任务]
    /// </summary>
    /// <returns></returns>
    List<BizTaskBean> GetAllExecuteTaskByEquipIdV2(long equipId);
    /// <summary>
    /// 根据任务号获得任务
    /// </summary>
    /// <param name="taskNo">任务号</param>
    /// <returns></returns>
    BizTaskBean GetTaskBeanByTaskNo(int taskNo);
    BizTaskBean GetTaskBeanByTaskNoV3(int taskNo);
    /// <summary>
    /// 通过任务ID获取任务
    /// </summary>
    /// <param name="taskId"></param>
    /// <returns></returns>
    BizTaskBean GetTaskBeanByTaskId(long taskId);
    /// <summary>
    /// 根据条码获得任务
    /// </summary>
    /// <param name="productCode"></param>
    /// <returns></returns>
    BizTaskBean GetTaskBeanByProductCode(string productCode);


    BizTaskBean GetEmsTaskBeanByTaskNo(int taskNo, long locId = 0);
    /// <summary>
    /// 获取Ems小车任务
    /// </summary>
    /// <param name="emsId"></param>
    /// <returns></returns>
    BizEmsTask GetTaskCmdByEmsId(long emsId);

    /// <summary>
    /// 获取任务取胎、放胎 XZ
    /// </summary>
    /// <param name="taskCmdId"></param>
    /// <param name="emsId"></param>
    /// <returns></returns>
    BizTaskCmdXZ GetTaskCmdXZ(long taskCmdId, long emsId);
    /// <summary>
    /// 根据条码获得任务
    /// </summary>
    /// <param name="productCode"></param>
    /// <returns></returns>
    TaskInfo GetTaskByProductCode(string productCode);
    /// <summary>
    /// 修改指令状态
    /// </summary>
    /// <param name="locId"></param>
    /// <param name="taskId"></param>
    /// <param name="status"></param>
    /// <returns></returns>
    int UpdateTaskCmdStatus(long locId, long taskId, int status);
    /// <summary>
    /// 修改备注
    /// </summary>
    /// <param name="taskNo"></param>
    /// <param name="remark"></param>
    /// <returns></returns>
    int UpdateTaskRemark(int taskNo, string remark);
    /// <summary>
    /// 获取要创建任务类型
    /// 0:不操作
    /// 1:创建普通出库
    /// 2:创建线体异常出库
    /// 3:堆垛机异常
    /// 4: 指令重下
    /// </summary>
    /// <param name="locId"></param>
    /// <returns></returns>
    TaskStatusBean GetTaskTypeByLocId(long locId);
    /// <summary>
    /// 通过位置获取信息
    /// </summary>
    /// <param name="locId"></param>
    /// <returns></returns>
    BizTaskBean GetTaskByLocId(long locId);
    /// <summary>
    /// 开始位置和流水号查询
    /// </summary>
    /// <param name="startLoc"></param>
    /// <param name="taskNo"></param>
    /// <returns></returns>

    bool GetTaskByStartLocAndCod(long startLoc, int taskNo);
    /// <summary>
    /// 修改上次执行类型
    /// </summary>
    /// <param name="equipId"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    int SetCrnType(long equipId, int type);

    /// <summary>
    /// 删除任务备份数据
    /// </summary>
    /// <returns></returns>
    int DeleteBakTask();
    /// <summary>
    /// 根据Id获取任务
    /// </summary>
    /// <param name="taskId"></param>
    /// <returns></returns>
    POWcsTask GetTaskById(long? taskId);
    /// <summary>
    /// 该任务指令是否需要结束
    /// </summary>
    /// <param name="task"></param>
    /// <param name="cmd"></param>
    /// <returns></returns>
    bool IsNeedFinish(POWcsTask task, POWcsTaskCmd cmd);

    /// <summary>
    /// 通过扫描头获取所有可执行任务
    /// </summary>
    /// <param name="StartLoc"></param>
    /// <returns></returns>
    BizTaskBeanV2 GetTaskByExecuteStartLoc(long StartLoc);

    BizTaskBeanV2 GetTaskByCmdExecuteStartLoc(long StartLoc);
    BizTaskBeanV2 GetTaskByExecuteStartLocV2(long StartLoc);
    List<BizTaskBeanV2> GetTaskByExecuteStartLocV3(long StartLoc);
    /// <summary>
    /// 修改任务的下发状态
    /// </summary>
    /// <param name="taskNo"></param>
    /// <param name="down"></param>
    /// <returns></returns>
    int UpdateTaskDown(int? taskNo, int down);
    /// <summary>
    /// 结束异常口的任务(含推送mes)
    /// </summary>
    /// <param name="taskNo"></param>
    /// <returns></returns>
    int FinishAbnormOutTask(int taskNo);


    POWcsAsrsCrnOutput GetAsrsCrnOutputByOutPutLocId(long targetLoc);
    /// <summary>
    /// 定时同步堆垛机任务
    /// </summary>
    /// <returns></returns>
    int SyncScadaCrnTask();
    /// <summary>
    /// 人工删除任务或者任务完成任务
    /// </summary>
    /// <returns></returns>
    int DeleteOrCompleteHandTask();
    /// <summary>
    /// 更新胎高
    /// </summary>
    /// <param name="materialCode"></param>
    /// <param name="height"></param>

    void UpdateHeight(string materialCode, string height);
}
/// <summary>
/// 任务对象
/// </summary>
public class BizTaskBean
{
    /// <summary>
    /// 任务ID
    /// </summary>
    public long? Id { get; set; }
    /// <summary>
    /// 任务流水号
    /// </summary>
    public int? TaskNo { get; set; }
    /// <summary>
    /// 状态
    /// </summary>
    public int? Status { get; set; }
    /// <summary>
    /// SKUID
    /// </summary>
    public long SkuId { get; set; }
    /// <summary>
    /// 任务类型(指令)
    /// </summary>
    public int? CmdType { get; set; }
    /// <summary>
    /// 任务类型
    /// </summary>
    public int? TaskType { get; set; }
    /// <summary>
    /// 起始位置ID
    /// </summary>
    public long? StartLoc { get; set; }
    /// <summary>
    /// 起始位置ID
    /// </summary>
    public string? StartCmdPlcLoc { get; set; }
    /// <summary>
    /// 执行的货叉
    /// </summary>
    public long? StartEquip { get; set; }
    /// <summary>
    /// 目标位置ID
    /// </summary>
    public long? TargetLoc { get; set; }
    /// <summary>
    /// 目标地址
    /// </summary>
    public string TargetPlcLoc { get; set; }
    /// <summary>
    /// 目标地址
    /// </summary>
    public string TargetCmdPlcLoc { get; set; }
    /// <summary>
    /// 是否喷涂
    /// </summary>
    //public int? IsSpray { get; set; }
    /// <summary>
    /// 旋转角度
    /// </summary>
    //public decimal? SpinAngle { get; set; }
    /// <summary>
    /// 是否翻转[0 翻胎，1 不翻]
    /// </summary>
    //public int? OverTurn { get; set; }
    /// <summary>
    /// 组号
    /// </summary>
    public int? TaskGroup { get; set; }
    /// <summary>
    /// 组号顺序
    /// </summary>
    public int? TaskGroupIndex { get; set; }
    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime? CreateTime { get; set; }
    /// <summary>
    /// 是否等待【0 不等待；1 等待；2 匹配完成】
    /// </summary>
    public int PairWait { get; set; }
    /// <summary>
    /// 外径
    /// </summary>
    public int OutSize { get; set; }
    /// <summary>
    /// 喷涂 10 喷涂；20 不喷涂
    /// </summary>
    //public int Paint { get; set; }
    /// <summary>
    /// 硫化机时间(秒)
    /// </summary>
    public int? CuringTime { get; set; }
    /// <summary>
    /// 指令状态
    /// </summary>
    public int? CmdStatus { get; set; }
    ///// <summary>
    ///// 任务状态
    ///// </summary>
    //public int? TaskStatus { get; set; }
    /// <summary>
    /// 胎高
    /// 
    /// </summary>
    //public int Height { get; set; }

    /// <summary>
    /// 条码
    /// </summary>
    public string ProductCode { get; set; }
    public string Remark { get; set; }

}



public class BizTaskBeanV2
{


    /// <summary>
    /// 任务流水号
    /// </summary>
    public int? TaskNo { get; set; }
    /// <summary>
    /// 目标地址
    /// </summary>
    public string TargetPlcLoc { get; set; }
    /// <summary>
    /// 指令目标地址
    /// </summary>
    public string TargetCmdPlcLoc { get; set; }


    public int OutSize { get; set; }

    /// <summary>
    /// 条码
    /// </summary>
    public string ProductCode { get; set; }

    public int TaskType { get; set; }

    public DateTime? CreateTime { get; set; }

    public int? CmdStatus { get; set; }

    public int? TaskGroup { get; set; }

}
/// <summary>
/// 任务信息
/// </summary>
public class TaskInfo
{
    /// <summary>
    /// 任务ID
    /// </summary>
    public long? Id { get; set; }
    /// <summary>
    /// 任务编号
    /// </summary>
    public int? TaskNo { get; set; }
}
/// <summary>
/// Ems任务信息
/// </summary>
public class BizEmsTask
{
    public long CmdId { get; set; }
    /// <summary>
    /// 流水号
    /// </summary>
    public int TaskNo { get; set; }
    /// <summary>
    /// 是否已执行【true 是；false 否】
    /// </summary>
    public bool IsExecuted { get; set; }
    /// <summary>
    /// 等待
    /// </summary>
    public int PairWait { get; set; }
}


/// <summary>
/// 指令对象
/// </summary>
public class BizTaskCmdXZBean
{

    public long EmsId { get; set; }
    public int TaskNo { get; set; }
    /// <summary>
    /// 取胎台号
    /// </summary>
    public decimal StartX { get; set; }
    /// <summary>
    /// 取胎高度
    /// </summary>
    public decimal StartZ { get; set; }
    /// <summary>
    /// 子口尺寸
    /// </summary>
    public decimal SubSize { get; set; }
    /// <summary>
    /// 放胎台号
    /// </summary>
    public decimal TargetX { get; set; }
    /// <summary>
    /// 放胎高度
    /// </summary>
    public decimal TargetZ { get; set; }

    /// <summary>
    /// 是否等待【0 不等待；1 等待；2 匹配完成】
    /// </summary>
    public int PairWait { get; set; }
    /// <summary>
    /// 是否异常【true 是；false 否】
    /// </summary>
    public bool IsAbnormal { get; set; }
    /// <summary>
    /// 开始位置
    /// </summary>
    public long StartLoc { get; set; }
    /// <summary>
    /// 目标位置
    /// </summary>
    public long TargetLoc { get; set; }
    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime? CreateTime { get; set; }
    /// <summary>
    /// 指令类型
    /// </summary>
    public int? CmdType { get; set; }


}
public class TaskStatusBean
{
    public TaskStatusBean()
    {
    }

    public TaskStatusBean(int sign, long taskId)
    {
        Sign = sign;
        TaskId = taskId;
    }

    /// <summary>
    /// 标志
    /// 0:不操作
    /// 1:创建普通出库
    /// 2:创建线体异常出库
    /// 3:堆垛机异常
    /// 4: 指令重下
    /// </summary>
    public int Sign { get; set; }
    /// <summary>
    /// 任务ID
    /// </summary>
    public long TaskId { get; set; }
}
/// <summary>
/// 指令信息
/// </summary>
public class BizTaskCmdXZ
{

    public long EmsId { get; set; }
    public int TaskNo { get; set; }
    /// <summary>
    /// 取胎台号
    /// </summary>
    public decimal StartX { get; set; }
    /// <summary>
    /// 取胎高度
    /// </summary>
    public decimal StartZ { get; set; }
    /// <summary>
    /// 子口尺寸
    /// </summary>
    public decimal SubSize { get; set; }
    /// <summary>
    /// 放胎台号
    /// </summary>
    public decimal TargetX { get; set; }
    /// <summary>
    /// 放胎高度
    /// </summary>
    public decimal TargetZ { get; set; }

    /// <summary>
    /// 是否等待【0 不等待；1 等待；2 匹配完成】
    /// </summary>
    public int PairWait { get; set; }
    /// <summary>
    /// 是否异常【true 是；false 否】
    /// </summary>
    public bool IsAbnormal { get; set; }
    /// <summary>
    /// 开始位置
    /// </summary>
    public long StartLoc { get; set; }
    /// <summary>
    /// 目标位置
    /// </summary>
    public long TargetLoc { get; set; }
    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime? CreateTime { get; set; }

    public int? CmdType { get; set; }


    public long EmsIdB { get; set; }
    /// <summary>
    /// 配对胎信息 ↓
    /// </summary>

    public int TaskNoB { get; set; }
    /// <summary>
    /// 取胎台号
    /// </summary>
    public decimal StartBX { get; set; }
    /// <summary>
    /// 取胎高度
    /// </summary>
    public decimal StartBZ { get; set; }
    /// <summary>
    /// 子口尺寸
    /// </summary>
    public decimal SubSizeB { get; set; }
    /// <summary>
    /// 放胎台号
    /// </summary>
    public decimal TargetBX { get; set; }
    /// <summary>
    /// 放胎高度
    /// </summary>
    public decimal TargetBZ { get; set; }
    /// <summary>
    /// 开始位置
    /// </summary>
    public long StartLocB { get; set; }
    /// <summary>
    /// 目标位置
    /// </summary>
    public long TargetLocB { get; set; }

    public int ASafe { get; set; }
    public int BSafe { get; set; }
}